Object
is the class that all other classes have as an ancestor.
Vector
Objects
To declare a reference variable for a Vector
, do this:
Vector myVector; // myVector is a reference to a future Vector object
You do not say what type of object you are intending to store.
A Vector
is like an array of references to Object
.
This means that any object reference can be stored in a Vector
.
To declare a variable and to construct a Vector
with an unspecified
initial capacity do this:
Vector myVector = new Vector(); // myVector is a reference to a Vector object. // The Java system picks the initial capacity.
This may not be very efficient.
If you have an idea of what the capacity you need,
start your Vector
with that capacity.
To declare a variable and to construct a Vector
with an
initial capacity of 15 do this:
Vector myVector = new Vector(15); // myVector is a reference to a Vector object // with an initial capacity of 15 elements.
The initial capacity is the size that the Vector
starts with.
It can expand beyond this capacity if you add more elements.
Expanding the capacity of a Vector
is slow;
and will slow down your program if it happens too often.
For more control, specify how many new slots will be added
each time the Vector
expands:
Vector myVector = new Vector(15, 5); // myVector is a reference to a Vector object // with an initial capacity of 15 elements, // and an increment size of 5.
Now when additional capacity is needed, 5 slots at a time will be added. You don't have to fill the new slots, but now they are there if you need them.
You are writing a program to keep track of the students in a class.
There are usually about 12 students in a class,
but often a few students add in during the first few weeks.
Declare and construct a Vector
suitable for this situation.
Vector csClass = new Vector( __________, __________ ) ;